package pl.edu.mimuw.irs.rcp.controller.editor.requirements;

import org.eclipse.gef.EditDomain;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import pl.edu.mimuw.irs.core.model.design.resource.IMember;
import pl.edu.mimuw.irs.core.model.design.resource.Requirement;
import pl.edu.mimuw.irs.rcp.controller.editor.util.AbstractAdaptableDataWrapper;
import pl.edu.mimuw.irs.rcp.controller.editor.util.IMemberWrapper;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;


public class AdaptableRequirementWrapper extends AbstractAdaptableDataWrapper<Requirement> implements IMemberWrapper {

	// *** Attributes
	
	private RequirementPropertySource requirementSource;
	public IMember getMember() {
		return getData();
	}
	
	// *** Constructors

	public AdaptableRequirementWrapper(Requirement data, EditDomain editDomain) {
		super(data, editDomain);
	}
	
	// *** Methods
	
	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySource.class) {
			if (requirementSource == null) {
				requirementSource = new RequirementPropertySource(
						getData().getName());
			}
			return requirementSource;
		}
		return null;
	}
	
	public class RequirementPropertySource implements IPropertySource {

		// *** Attributes
		
		private static final String PROPERTY_NAME = "properties.requirement.name";
		
		private IPropertyDescriptor[] propertyDescriptors;
		
		private String defaultName;
		
		// *** Constructors
		
		public RequirementPropertySource(String defaultName) {
			this.defaultName = defaultName;
		}
	
		// *** Methods
		
		@Override
		public Object getEditableValue() {
			return null;
		}

		@Override
		public IPropertyDescriptor[] getPropertyDescriptors() {
			if (propertyDescriptors == null) {
	            // Create a descriptors and set a category
	            PropertyDescriptor nameDescriptor = 
	            	new TextPropertyDescriptor(PROPERTY_NAME, UITexts.get(MessageCode.PROPERTIES_FIELD_NAME));

	            propertyDescriptors = new IPropertyDescriptor[] { 
	            		nameDescriptor };
            }
            return propertyDescriptors;
		}

		@Override
		public Object getPropertyValue(Object id) {
			if (PROPERTY_NAME.equals(id)) {
				return getData().getName();
			}
			return null;
		}

		@Override
		public boolean isPropertySet(Object id) {
			if (PROPERTY_NAME.equals(id)) {
	        	String curValue = (String)getPropertyValue(id);
				return !curValue.equals(defaultName);
			}
			return false;
		}

		@Override
		public void resetPropertyValue(Object id) {
			if (PROPERTY_NAME.equals(id)) {
				setPropertyValue(id, defaultName);
			}
		}

		@Override
		public void setPropertyValue(Object id, Object value) {
			if (PROPERTY_NAME.equals(id)) {
				getData().setName((String)value);
			}
		}
		
	}
}
